import React from 'react'
import { Form, Input, InputNumber, Upload, Button, message } from 'antd'
import { UploadOutlined } from '@ant-design/icons'
import request from '@/utils/request'
const { TextArea } = Input;

export default (props: any) => {
    const { form, getList, id, close } = props
    const handleUploadChange = (info: any): any => {
        let fileList = info.fileList;
        fileList = fileList.slice(-1);
        if (info.file.status === 'done') {
            message.success(`${info.file.name}文件上传成功`);
            fileList[0].url = fileList[0]['response']['data']['filePath'];
        }
        return fileList
    };
    const onFinish = (values: any) => {
        if (values.upload && values.upload.length > 0) {
            values.logo = values.upload[0]['url'];
        }
        request(`/goods/tag/${id}`, {
            method: 'POST',
            data: { ...values },
        }).then((result) => {
            if (result.code !== 200) {
                message.warning(result.message)
                return
            }
            message.success(result.message)
            close()
            getList()
        })
    }
    return (
        <div>
            <Form
                labelCol={{ span: 4 }}
                wrapperCol={{ span: 16 }}
                onFinish={onFinish}
                form={form}
            >
                <Form.Item label="标签名称" required name="title"      rules={[{ required: true, message: '请填写标签名称' }]}>
                    <Input />
                </Form.Item>
                <Form.Item label="排序" name="sort">
                    <InputNumber />
                </Form.Item>
                <Form.Item label="图片" name="upload" valuePropName="fileList" getValueFromEvent={handleUploadChange}>
                    <Upload
                        action='http://127.0.0.1:8888/admin/tool/upload'
                        listType='picture'
                    >
                        <Button>
                            <UploadOutlined /> Upload
                        </Button>
                    </Upload>
                </Form.Item>
                <Form.Item label="标签描述" name="describe">
                    <TextArea rows={3} />
                </Form.Item>
            </Form>
        </div>
    );
}